package cn.darkhorse.mesadmin.task;

import cn.darkhorse.common.util.HttpClientUtils;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.pick.pojo.CottonApiModel;
import cn.darkhorse.pick.pojo.PickInfo;
import cn.darkhorse.pick.pojo.PickMapInfo;
import cn.darkhorse.pick.service.IPickInfoService;
import cn.darkhorse.pick.service.IPickMapInfoService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.ApiConfig;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.Year;
import java.util.ArrayList;
import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;

/**
 * 第三方系统数据处理定时任务
 * @author yuanjun
 * @since 20231108
 */

@Component("GetPickInfoTask")
public class GetPickInfoTask {

    @Resource
    private ApiConfig apiConfig;

    @Resource
    private MesDeviceService mesDeviceService;

    @Resource
    private IPickInfoService pickInfoService;


    /**
     * 定时任务拉取采摘信息
     * @param year
     */
    public void doParse(String year) {
        List<MesDevice> deviceList = mesDeviceService.list();
        LocalDateTime now = LocalDateTime.now();
        // 数据范围默认年份为今年
        if (year == null){
            year = now.getYear()+ "";
        }
        String startTime = year +"-01-01 00:00:00";
        String endTime = year +"-12-31 23:59:59";

        String URL = "https://zs.ecottone.com/api/getPickInfoList";

        ArrayList<PickInfo> pickInfos = new ArrayList<>();

        for (MesDevice device : deviceList) {
            String code = device.getCode();
            JSONObject object = new JSONObject();

            object.put("deviceMainCode",code);
            object.put("startTime",startTime);
            object.put("endTime",endTime);
            String s = sendPost(URL, object);

            JSONObject parse = JSONObject.parse(s);
            JSONArray array = parse.getJSONArray("data");

            for (int i = 0; i < array.size(); i++) {
                PickInfo info = array.getObject(i, PickInfo.class);
                pickInfos.add(info);
            }
        }

        pickInfoService.saveOrUpdateBatch(pickInfos);


    }


    @NotNull
    private String sendPost(String URL, JSONObject object) {
        String timedStamp = timeStamp();
        String sign = Md5Utils.hash(apiConfig.getServiceName() + apiConfig.getKey() + timedStamp);
        CottonApiModel cottonApiModel = new CottonApiModel(apiConfig.getServiceName(), timedStamp, sign, JSONObject.toJSONString(object));
        String jsonString = JSONObject.toJSONString(cottonApiModel);
        System.out.println(jsonString);
        String s = null;
        try {
            s = HttpClientUtils.doPostJson(URL, JSONObject.toJSONString(cottonApiModel));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return s;
    }


    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }
}
